﻿using Microsoft.EntityFrameworkCore;
using mozhi.smarterp.Business.EntityFrameworkCore;
using mozhi.smarterp.Business.Inventories;
using mozhi.smarterp.Business.Inventories.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace mozhi.smarterp.Business.Repositories.Inventory
{
    public class InventoryRealTimeRepository : EfCoreRepository<BusinessDbContext, InventoryRealTime, Guid>, IInventoryRealTimeRepository
    {
        public InventoryRealTimeRepository(IDbContextProvider<BusinessDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        public Task<InventoryRealTime> GetAsync(Guid? productId, Guid? warehouseId, Guid? locationId, string batchNumber, string serialNumber, DateTime? productionDate , CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }

        public async Task<int> GetCountAsync(Guid? productId, Guid? warehouseId, Guid? locationId, string batchNumber, string serialNumber, DateTime? productionDate, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var count = await dbContext.Set<InventoryRealTime>().AsNoTracking()
                .WhereIf(productId.HasValue, x => x.ProductId == productId)
                .WhereIf(warehouseId.HasValue, x => x.WarehouseId == warehouseId)
                .WhereIf(locationId.HasValue, x => x.LocationId == warehouseId)
                .WhereIf(!batchNumber.IsNullOrEmpty(), x => x.BatchNumber == batchNumber)
                .WhereIf(!serialNumber.IsNullOrEmpty(), x => x.SerialNumber == serialNumber)
                .WhereIf(productionDate.HasValue, x => x.ProductionDate == productionDate)
                .CountAsync(cancellationToken);

            return count;
        }

        public async Task<List<InventoryRealTime>> GetListAsync(Guid? productId, Guid? warehouseId, Guid? locationId, string batchNumber, string serialNumber, DateTime? productionDate, CancellationToken cancellationToken = default)
        {
            var dbContext = await GetDbContextAsync();

            var inventories = await dbContext.Set<InventoryRealTime>().AsNoTracking()
                .WhereIf(productId.HasValue, x => x.ProductId == productId)
                .WhereIf(warehouseId.HasValue, x => x.WarehouseId == warehouseId)
                .WhereIf(locationId.HasValue, x => x.LocationId == warehouseId)
                .WhereIf(!batchNumber.IsNullOrEmpty(), x => x.BatchNumber == batchNumber)
                .WhereIf(!serialNumber.IsNullOrEmpty(), x => x.SerialNumber == serialNumber)
                .WhereIf(productionDate.HasValue, x => x.ProductionDate == productionDate)
                .ToListAsync(cancellationToken);

            return inventories;
        }
    }
}
